﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainProGrid
{
    public partial class ReferenceFrm : Form
    {
        public ReferenceFrm(string mes)
        {
            InitializeComponent();
            this.MyTxt.Text = mes;
            string[] keys = { "script", "type", "src" };
            int index = this.MyTxt.SelectionStart;    //记录修改的位置
            this.MyTxt.SelectAll();
            this.MyTxt.SelectionColor = Color.White;
            for (int i = 0; i < keys.Length; i++)
            {
                this.getbunch(keys[i], this.MyTxt.Text,Color.Red);
            }
            string[] keys1 = { "text/javascript", "${rootcomponent}"};
            for (int i = 0; i < keys1.Length; i++)
            {
                this.getbunch(keys1[i], this.MyTxt.Text, Color.Blue);
            }
            this.MyTxt.Select(index, 0);
            this.MyTxt.SelectionColor = Color.White;
        }
        public int getbunch(string p, string s,Color color)  //给关键字上色
        {
            int cnt = 0;
            int M = p.Length;
            int N = s.Length;
            char[] ss = s.ToCharArray(), pp = p.ToCharArray();
            if (M > N) return 0;
            for (int i = 0; i < N - M + 1; i++)
            {
                int j;
                for (j = 0; j < M; j++)
                {
                    if (ss[i + j] != pp[j]) break;
                }
                if (j == p.Length)
                {
                    this.MyTxt.Select(i, p.Length);
                    this.MyTxt.SelectionColor = color;
                    cnt++;
                }
            }
            return cnt;
        }
    }
}
